package com.glory.wxmemsicpda.base

import android.app.Application
import android.content.Context
import com.glory.wxmemsicpda.utils.MyUtils
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_InitUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers

class App : Application() {
    override fun onCreate() {
        super.onCreate()
        instance = this
        app = applicationContext
        val timeOut = FreeApi_SharePreferencesUtils.getSharePre(
            "TIME_OUT_FILE",
            "TIME_OUT2",
            applicationContext
        )
        if (timeOut.isNotEmpty()) {
            Constant.DEFAULT_TIME_OUT = timeOut
        } else {
            FreeApi_SharePreferencesUtils.setSharePre(
                "TIME_OUT_FILE",
                "TIME_OUT2",
                Constant.DEFAULT_TIME_OUT,
                applicationContext
            )
        }
        FreeUI_InitUtils.init(
            applicationContext,
            this,
            false,
            Constant.DEFAULT_TIME_OUT.toInt() * 1000L,
            0
        )
        FreeUI_InitUtils.setTheme(FreeApi_StaticMembers.THEME_GLORY)
        Constant.MAC_ADDR = MyUtils.getMac(this)!!
        val ip = FreeApi_SharePreferencesUtils.getSharePre("URL_FILE", "URL_IP", applicationContext)
        if (ip.isNotEmpty()) {
            Constant.REQUEST_URL = ip
        } else {
            FreeApi_SharePreferencesUtils.setSharePre(
                "URL_FILE",
                "URL_IP",
                Constant.REQUEST_URL,
                applicationContext
            )
        }
        val mesIp = FreeApi_SharePreferencesUtils.getSharePre("URL_FILE", "MES_IP", applicationContext)
        if (mesIp.isNotEmpty()) {
            Constant.MES_REQUST_URL = mesIp
        } else {
            FreeApi_SharePreferencesUtils.setSharePre(
                "URL_FILE",
                "MES_IP",
                Constant.MES_REQUST_URL,
                applicationContext
            )
        }
        val fileIP =
            FreeApi_SharePreferencesUtils.getSharePre("URL_FILE", "FILE_IP", applicationContext)
        if (fileIP.length > 0) {
            Constant.FILE_URL = fileIP
        } else {
            FreeApi_SharePreferencesUtils.setSharePre(
                "URL_FILE",
                "FILE_IP",
                Constant.FILE_URL,
                applicationContext
            )
        }
    }

    companion object {
        var app: Context? = null
        var instance: App? = null
            private set
    }
}